package com.itheima.service.Impl;

import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    /**
     * 批量导入预约设置
     * @param list
     */
    @Override
    public void add(List<OrderSetting> list) throws ParseException {
        //日期短格式
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        for(int i=list.size()-1;i>=0;--i){
        //for (OrderSetting orderSetting : list) {
            OrderSetting orderSetting= list.get(i);
            //将Date转换为String
            String dateString = formatter.format(orderSetting.getOrderDate());
            //将String转换为Date
            Date date = formatter.parse(dateString);
            //检查此数据（日期）是否存在
            long count = orderSettingMapper.findCountByOrderDate(date);
            if(count > 0){
                //已经存在，执行更新操作
                orderSettingMapper.editNumberByOrderDate(orderSetting);
                list.remove(orderSetting);
            }
        }
        //不存在，执行批量添加操作
        if(list != null && list.size() > 0) {
            orderSettingMapper.add(list);
        }
    }

    /**
     * 根据日期查询预约设置数据(获取指定日期所在月份的预约设置数据)
     * @param month
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String month) {//2019-3
        String dateBegin = month + "-1";//2019-3-1
        String dateEnd = month + "-31";//2019-3-31
        Map map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
        List<OrderSetting> list = orderSettingMapper.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());//获得日期（几号）
            orderSettingMap.put("number",orderSetting.getNumber());//可预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());//已预约人数
            data.add(orderSettingMap);
        }
        return data;
    }

    /**
     * 根据指定日期修改可预约人数
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        //将Date转换为String
        String dateString = formatter.format(orderSetting.getOrderDate());
        //将String转换为Date
        Date date = formatter.parse(dateString);
        orderSetting.setOrderDate(date);
        //判断当前日期是否已经进行了预约设置
        long count = orderSettingMapper.findCountByOrderDate(date);
        log.info("count:{}",count);
        if(count > 0){
            //已经 did
            orderSettingMapper.editNumberByOrderDate(orderSetting);
        }else{
            //没有
            List<OrderSetting> list = new ArrayList<>();
            list.add(orderSetting);
            orderSettingMapper.add(list);
        }
    }
}
